#include "circlewizardpage.h"

#include "../talent.h"
#include "../database.h"

CircleWizardPage::CircleWizardPage(int page, QWidget *parent) :
  QWizardPage(parent),
  ui(new Ui::CircleWizardPage)
{
  ui->setupUi(this);
  setTitle("Cercle " + QString::number(page));

  foreach(const Talent& t, Database::talents())
  {
    ui->allTalents->addItem(t.name());
  }

  connect(ui->talentFilter,SIGNAL(textChanged(QString)),this,SLOT(talentFilterChanged(QString)));
  connect(ui->addTalent,SIGNAL(clicked()),this,SLOT(addTalent()));
  connect(ui->removeTalent,SIGNAL(clicked()),this,SLOT(removeTalent()));
  connect(ui->addKarma,SIGNAL(clicked()),this,SLOT(addKarma()));
  connect(ui->removeKarma,SIGNAL(clicked()),this,SLOT(removeKarma()));
}

CircleWizardPage::~CircleWizardPage()
{
  delete ui;
}

void CircleWizardPage::talentFilterChanged(const QString &f)
{
  for (int i=0;i<ui->allTalents->count();++i)
  {
    QListWidgetItem* item = ui->allTalents->item(i);
    item->setHidden(!item->text().contains(f,Qt::CaseInsensitive));
  }
}

void CircleWizardPage::addTalent()
{
  if (ui->allTalents->selectedItems().size()==0) return;
  QString text = ui->allTalents->selectedItems().first()->text();
  delete ui->allTalents->selectedItems().first();
  QListWidgetItem* item = new QListWidgetItem(text);
  item->setCheckState(Qt::Unchecked);
  item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  ui->talents->addItem(item);
  ui->talents->sortItems(Qt::AscendingOrder);
  ui->allTalents->sortItems(Qt::AscendingOrder);
}

void CircleWizardPage::removeTalent()
{
  if (ui->talents->selectedItems().size()==0) return;
  QString txt = ui->talents->selectedItems().first()->text();
  delete ui->talents->selectedItems().first();
  ui->allTalents->addItem(txt);
  talentFilterChanged(ui->talentFilter->text());
  ui->talents->sortItems(Qt::AscendingOrder);
  ui->allTalents->sortItems(Qt::AscendingOrder);
}

void CircleWizardPage::addKarma()
{
  QListWidgetItem* item = new QListWidgetItem("Karma");
  item->setFlags(item->flags() | Qt::ItemIsEditable);
  ui->karmaUsage->addItem(item);
}

void CircleWizardPage::removeKarma()
{
  if (ui->karmaUsage->selectedItems().size()==0) return;
  delete ui->karmaUsage->selectedItems().first();
}
